{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b4a86c30-6eb0-451e-ba52-4cd01df2df52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/\n",
      "Requirement already satisfied: pymilvus==2.5.10 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]==2.5.10) (2.5.10)\n",
      "Requirement already satisfied: openai==1.82.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (1.82.0)\n",
      "Requirement already satisfied: requests==2.32.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (2.32.3)\n",
      "Requirement already satisfied: tqdm==4.67.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (4.67.1)\n",
      "Requirement already satisfied: torch==2.7.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (2.7.0)\n",
      "Requirement already satisfied: setuptools>69 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (78.1.1)\n",
      "Requirement already satisfied: grpcio<=1.67.1,>=1.49.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (1.67.1)\n",
      "Requirement already satisfied: protobuf>=3.20.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (6.31.1)\n",
      "Requirement already satisfied: python-dotenv<2.0.0,>=1.0.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (1.1.1)\n",
      "Requirement already satisfied: ujson>=2.0.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (5.10.0)\n",
      "Requirement already satisfied: pandas>=1.2.4 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.3.0)\n",
      "Requirement already satisfied: milvus-lite>=2.4.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.5.1)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (0.10.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (2.11.7)\n",
      "Requirement already satisfied: sniffio in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (1.3.1)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (4.14.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (3.4.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (3.10)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (2.5.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (2025.6.15)\n",
      "Requirement already satisfied: filelock in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.18.0)\n",
      "Requirement already satisfied: sympy>=1.13.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (1.14.0)\n",
      "Requirement already satisfied: networkx in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.5)\n",
      "Requirement already satisfied: jinja2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.1.6)\n",
      "Requirement already satisfied: fsspec in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (2025.5.1)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.77)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.77)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.80)\n",
      "Requirement already satisfied: nvidia-cudnn-cu12==9.5.1.17 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (9.5.1.17)\n",
      "Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.4.1)\n",
      "Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (11.3.0.4)\n",
      "Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (10.3.7.77)\n",
      "Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (11.7.1.2)\n",
      "Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.5.4.2)\n",
      "Requirement already satisfied: nvidia-cusparselt-cu12==0.6.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (0.6.3)\n",
      "Requirement already satisfied: nvidia-nccl-cu12==2.26.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (2.26.2)\n",
      "Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.77)\n",
      "Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.85)\n",
      "Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (1.11.1.6)\n",
      "Requirement already satisfied: triton==3.3.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.3.0)\n",
      "Requirement already satisfied: pymilvus.model>=0.3.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]==2.5.10) (0.3.2)\n",
      "Requirement already satisfied: httpcore==1.* in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai==1.82.0) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai==1.82.0) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (0.4.1)\n",
      "Requirement already satisfied: numpy>=1.26.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.3.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2025.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2025.2)\n",
      "Requirement already satisfied: transformers>=4.36.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (4.53.1)\n",
      "Requirement already satisfied: onnxruntime in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (1.22.0)\n",
      "Requirement already satisfied: scipy>=1.10.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (1.16.0)\n",
      "Requirement already satisfied: six>=1.5 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (1.17.0)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from sympy>=1.13.3->torch==2.7.0) (1.3.0)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.30.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (0.33.2)\n",
      "Requirement already satisfied: packaging>=20.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (25.0)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (6.0.2)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (2024.11.6)\n",
      "Requirement already satisfied: tokenizers<0.22,>=0.21 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (0.21.2)\n",
      "Requirement already satisfied: safetensors>=0.4.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (0.5.3)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from huggingface-hub<1.0,>=0.30.0->transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (1.1.5)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from jinja2->torch==2.7.0) (3.0.2)\n",
      "Requirement already satisfied: coloredlogs in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (15.0.1)\n",
      "Requirement already satisfied: flatbuffers in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (25.2.10)\n",
      "Requirement already satisfied: humanfriendly>=9.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from coloredlogs->onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (10.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "!pip install \"pymilvus[model]==2.5.10\" openai==1.82.0 requests==2.32.3 tqdm==4.67.1 torch==2.7.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2927d1b9-64cd-486d-9065-671ef2db0779",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 从环境变量获取 DeepSeek API Key\n",
    "api_key = os.getenv(\"DEEPSEEK_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5655f063-09d1-4364-97ec-758227de4302",
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "\n",
    "text_lines = []\n",
    "\n",
    "for file_path in glob(\"en/faq/*.md\", recursive=True):\n",
    "    with open(file_path, \"r\") as file:\n",
    "        file_text = file.read()\n",
    "\n",
    "    text_lines += file_text.split(\"# \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "591c9382-c377-4c2f-8785-475f2c981c5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "72"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text_lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "373aa29b-5007-49db-835e-414b9d32b162",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "deepseek_client = OpenAI(\n",
    "    api_key=api_key,\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek API 的基地址\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "626b33b6-46ba-436e-ac61-b5b3ff8b815c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/deepseek/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from pymilvus import model as milvus_model\n",
    "\n",
    "embedding_model = milvus_model.DefaultEmbeddingFunction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d388c44d-297c-4896-8f3b-fca6b18b8e91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "768\n",
      "[-0.04836059  0.07163021 -0.01130063 -0.03789341 -0.03320651 -0.01318453\n",
      " -0.03041721 -0.02269495 -0.02317858 -0.00426026]\n"
     ]
    }
   ],
   "source": [
    "test_embedding = embedding_model.encode_queries([\"This is a test\"])[0]\n",
    "embedding_dim = len(test_embedding)\n",
    "print(embedding_dim)\n",
    "print(test_embedding[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f09ab7d7-dd41-4308-bb58-61152a7b450f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.02752976  0.0608853   0.00388525 -0.00215193 -0.02774976 -0.0118618\n",
      " -0.04020916 -0.06023417 -0.03813156  0.0100272 ]\n"
     ]
    }
   ],
   "source": [
    "test_embedding_0 = embedding_model.encode_queries([\"That is a test\"])[0]\n",
    "print(test_embedding_0[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e60c6046-2f97-4297-ae3f-01e649188906",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import MilvusClient\n",
    "\n",
    "milvus_client = MilvusClient(uri=\"./milvus_demo.db\")\n",
    "\n",
    "collection_name = \"my_rag_collection\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "634ed395-6642-48c2-b15e-6afb2c776285",
   "metadata": {},
   "outputs": [],
   "source": [
    "if milvus_client.has_collection(collection_name):\n",
    "    milvus_client.drop_collection(collection_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d74417b8-0087-4243-aabe-5f7db03dbab5",
   "metadata": {},
   "outputs": [],
   "source": [
    "milvus_client.create_collection(\n",
    "    collection_name=collection_name,\n",
    "    dimension=embedding_dim,\n",
    "    metric_type=\"IP\",  # 内积距离\n",
    "    consistency_level=\"Strong\",  # 支持的值为 (`\"Strong\"`, `\"Session\"`, `\"Bounded\"`, `\"Eventually\"`)。更多详情请参见 https://milvus.io/docs/consistency.md#Consistency-Level。\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dd85661f-8a7d-41bd-9cc3-68ed849be51d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating embeddings: 100%|██████████| 72/72 [00:00<00:00, 313267.52it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'insert_count': 72, 'ids': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71], 'cost': 0}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "data = []\n",
    "\n",
    "doc_embeddings = embedding_model.encode_documents(text_lines)\n",
    "\n",
    "for i, line in enumerate(tqdm(text_lines, desc=\"Creating embeddings\")):\n",
    "    data.append({\"id\": i, \"vector\": doc_embeddings[i], \"text\": line})\n",
    "\n",
    "milvus_client.insert(collection_name=collection_name, data=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "361c74f1-4e6a-4d47-82a6-997b1fa5afde",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"Does Milvus support message engines other than Pulsar?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "342abce8-a875-4e25-afdd-c86d2cff6fe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_res = milvus_client.search(\n",
    "    collection_name=collection_name,\n",
    "    data=embedding_model.encode_queries(\n",
    "        [question]\n",
    "    ),  # 将问题转换为嵌入向量\n",
    "    limit=3,  # 返回前3个结果\n",
    "    search_params={\"metric_type\": \"IP\", \"params\": {}},  # 内积距离\n",
    "    output_fields=[\"text\"],  # 返回 text 字段\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d77d597a-b2bf-484e-bd7d-ca3853d6eb2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    [\n",
      "        \"Does Milvus support message engines other than Pulsar?\\n\\nYes. Kafka is supported in Milvus 2.1.0.\\n\\n###\",\n",
      "        0.8893545866012573\n",
      "    ],\n",
      "    [\n",
      "        \"Does Milvus support Apple M1 CPU?\\n\\nCurrent Milvus release does not support Apple M1 CPU.\\n\\n###\",\n",
      "        0.5475454330444336\n",
      "    ],\n",
      "    [\n",
      "        \"What data types does Milvus support on the primary key field?\\n\\nIn current release, Milvus supports both INT64 and string.\\n\\n###\",\n",
      "        0.5421543717384338\n",
      "    ]\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "retrieved_lines_with_distances = [\n",
    "    (res[\"entity\"][\"text\"], res[\"distance\"]) for res in search_res[0]\n",
    "]\n",
    "print(json.dumps(retrieved_lines_with_distances, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8d0aeaa0-0d46-40d8-956d-b0cb4ce1678f",
   "metadata": {},
   "outputs": [],
   "source": [
    "context = \"\\n\".join(\n",
    "    [line_with_distance[0] for line_with_distance in retrieved_lines_with_distances]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e9505466-c6eb-4fb9-8279-e88012ff3953",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Does Milvus support message engines other than Pulsar?\\n\\nYes. Kafka is supported in Milvus 2.1.0.\\n\\n###\\nDoes Milvus support Apple M1 CPU?\\n\\nCurrent Milvus release does not support Apple M1 CPU.\\n\\n###\\nWhat data types does Milvus support on the primary key field?\\n\\nIn current release, Milvus supports both INT64 and string.\\n\\n###'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ebf284b2-fe30-4436-8f24-8a1b5ed80f14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Does Milvus support message engines other than Pulsar?'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d2f032bf-d03d-4cda-8193-e8fa753aff1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "SYSTEM_PROMPT = \"\"\"\n",
    "Human: 你是一个 AI 助手。你能够从提供的上下文段落片段中找到问题的答案。\n",
    "\"\"\"\n",
    "USER_PROMPT = f\"\"\"\n",
    "请使用以下用 <context> 标签括起来的信息片段来回答用 <question> 标签括起来的问题。最后追加原始回答的中文翻译，并用 <translated>和</translated> 标签标注。\n",
    "<context>\n",
    "{context}\n",
    "</context>\n",
    "<question>\n",
    "{question}\n",
    "</question>\n",
    "<translated>\n",
    "</translated>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "94a07776-c17d-47f0-8a5a-40cee2b6539b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n请使用以下用 <context> 标签括起来的信息片段来回答用 <question> 标签括起来的问题。最后追加原始回答的中文翻译，并用 <translated>和</translated> 标签标注。\\n<context>\\nDoes Milvus support message engines other than Pulsar?\\n\\nYes. Kafka is supported in Milvus 2.1.0.\\n\\n###\\nDoes Milvus support Apple M1 CPU?\\n\\nCurrent Milvus release does not support Apple M1 CPU.\\n\\n###\\nWhat data types does Milvus support on the primary key field?\\n\\nIn current release, Milvus supports both INT64 and string.\\n\\n###\\n</context>\\n<question>\\nDoes Milvus support message engines other than Pulsar?\\n</question>\\n<translated>\\n</translated>\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "USER_PROMPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0ad16c28-463b-48e5-8b2f-6382812d413d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes. Kafka is supported in Milvus 2.1.0.\n",
      "\n",
      "<translated>\n",
      "是的。Milvus 2.1.0 版本支持 Kafka。\n",
      "</translated>\n"
     ]
    }
   ],
   "source": [
    "response = deepseek_client.chat.completions.create(\n",
    "    model=\"deepseek-chat\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
    "        {\"role\": \"user\", \"content\": USER_PROMPT},\n",
    "    ],\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65e9841e-419a-4493-9ab8-614d1738b5c2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
